package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.AddressDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = AddressService.class)
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressDao addressDao;

    /**
     * 条件查询出公司地址(带分页功能)
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //条件
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Address> page = addressDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除地址信息
     * @param address
     * @return
     */
    @Override
    public void delete(Address address) {
        addressDao.delete(address.getId());
    }

    /**
     * 添加地址
     * @param address
     * @return
     */
    @Override
    public void add(Address address) {
        addressDao.add(address);
    }

    /**
     * 查找所有的公司地址
     * @return
     */
    @Override
    public List<Address> findAllAddresses() {
        return addressDao.findByCondition(null).getResult();
    }
}